
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

public class CsvParser {

    private BufferedReader inStream = null;

    private Vector<String> vContent = null;

    public CsvParser(String csvFileName) throws FileNotFoundException {
        inStream = new BufferedReader(new FileReader(csvFileName));
    }

    public Vector<String> getLineContentVector() throws IOException, Exception {
        if (this.readCSVNextRecord()) {
            return this.vContent;
        }
        return null;
    }

    public void close() {
        if (inStream != null) {
            try {
                inStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public boolean readCSVNextRecord() throws IOException, Exception {

        if (inStream == null) {
            return false;
        }

        if (vContent == null) {
            vContent = new Vector<String>();
        }

        vContent.removeAllElements();

        String logicLineStr = "";

        StringBuilder strb = new StringBuilder();

        boolean isLogicLine = false;
        try {
            while (!isLogicLine) {
                String newLineStr = inStream.readLine();
                if (newLineStr == null) {
                    strb = null;
                    vContent = null;
                    isLogicLine = true;
                    break;
                }
                if (newLineStr.startsWith("#")) {

                    continue;
                }
                if (!strb.toString().equals("")) {
                    strb.append("/r/n");
                }
                strb.append(newLineStr);
                String oldLineStr = strb.toString();
                if (oldLineStr.indexOf(",") == -1) {

                    if (containsNumber(oldLineStr, "\"") % 2 == 0) {

                        isLogicLine = true;
                        break;
                    } else {
                        if (oldLineStr.startsWith("\"")) {
                            if (oldLineStr.equals("\"")) {
                                continue;
                            } else {
                                String tempOldStr = oldLineStr.substring(1);
                                if (isQuoteAdjacent(tempOldStr)) {

                                    continue;
                                } else {

                                    isLogicLine = true;
                                    break;
                                }
                            }
                        }
                    }
                } else

                {

                    String tempOldLineStr = oldLineStr.replace("\"\"", "");
                    int lastQuoteIndex = tempOldLineStr.lastIndexOf("\"");
                    if (lastQuoteIndex == 0) {
                        continue;
                    } else if (lastQuoteIndex == -1) {
                        isLogicLine = true;
                        break;
                    } else {
                        tempOldLineStr = tempOldLineStr.replace("\",\"", "");
                        lastQuoteIndex = tempOldLineStr.lastIndexOf("\"");
                        if (lastQuoteIndex == 0) {
                            continue;
                        }
                        if (tempOldLineStr.charAt(lastQuoteIndex - 1) == ',') {
                            continue;
                        } else {
                            isLogicLine = true;
                            break;
                        }
                    }
                }
            }
        } catch (

        IOException ioe) {
            ioe.printStackTrace();

            if (inStream != null) {
                inStream.close();
            }
            throw ioe;
        } catch (Exception e) {
            e.printStackTrace();

            if (inStream != null) {
                inStream.close();
            }
            throw e;
        }
        if (strb == null) {

            return false;
        }

        logicLineStr = strb.toString();
        if (logicLineStr != null) {

            while (!logicLineStr.equals("")) {
                String[] ret = readAtomString(logicLineStr);
                String atomString = ret[0];
                logicLineStr = ret[1];
                vContent.add(atomString);
            }
        }
        return true;
    }

    public String[] readAtomString(String lineStr) {
        String atomString = "";
        String orgString = "";
        String[] ret = new String[2];
        boolean isAtom = false;
        String[] commaStr = lineStr.split(",");
        while (!isAtom) {
            for (String str : commaStr) {
                if (!atomString.equals("")) {
                    atomString = atomString + ",";
                }
                atomString = atomString + str;
                orgString = atomString;
                if (!isQuoteContained(atomString)) {

                    isAtom = true;
                    break;
                } else {
                    if (!atomString.startsWith("\"")) {

                        isAtom = true;
                        break;
                    } else if (atomString.startsWith("\"")) {

                        if (containsNumber(atomString, "\"") % 2 == 0) {

                            String temp = atomString;
                            if (temp.endsWith("\"")) {
                                temp = temp.replace("\"\"", "");
                                if (temp.equals("")) {

                                    atomString = "";
                                    isAtom = true;
                                    break;
                                } else {

                                    temp = temp.substring(1, temp.lastIndexOf("\""));
                                    if (temp.indexOf("\"") > -1) {

                                        temp = atomString;
                                        temp = temp.substring(1);
                                        temp = temp.substring(0, temp.indexOf("\""))
                                                + temp.substring(temp.indexOf("\"") + 1);
                                        atomString = temp;
                                        isAtom = true;
                                        break;
                                    } else {

                                        temp = atomString;
                                        temp = temp.substring(1, temp.lastIndexOf("\""));
                                        temp = temp.replace("\"\"", "\"");
                                        atomString = temp;
                                        isAtom = true;
                                        break;
                                    }
                                }
                            } else {

                                temp = temp.substring(1, temp.indexOf('\"', 1))
                                        + temp.substring(temp.indexOf('\"', 1) + 1);
                                atomString = temp;
                                isAtom = true;
                                break;
                            }
                        } else

                        {

                            if (!atomString.equals("\"")) {
                                String tempAtomStr = atomString.substring(1);
                                if (!isQuoteAdjacent(tempAtomStr)) {

                                    tempAtomStr = atomString.substring(1);
                                    int tempQutoIndex = tempAtomStr.indexOf("\"");

                                    tempAtomStr = tempAtomStr.substring(0, tempQutoIndex)
                                            + tempAtomStr.substring(tempQutoIndex + 1);
                                    atomString = tempAtomStr;
                                    isAtom = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

        if (lineStr.length() > orgString.length()) {
            lineStr = lineStr.substring(orgString.length());
        } else {
            lineStr = "";
        }

        if (lineStr.startsWith(",")) {
            if (lineStr.length() > 1) {
                lineStr = lineStr.substring(1);
            } else {
                lineStr = "";
            }
        }
        ret[0] = atomString;
        ret[1] = lineStr;
        return ret;
    }

    public int containsNumber(String parentStr, String parameter) {
        int containNumber = 0;
        if (parentStr == null || parentStr.equals("")) {
            return 0;
        }
        if (parameter == null || parameter.equals("")) {
            return 0;
        }
        for (int i = 0; i < parentStr.length(); i++) {
            i = parentStr.indexOf(parameter, i);
            if (i > -1) {
                i = i + parameter.length();
                i--;
                containNumber = containNumber + 1;
            } else {
                break;
            }
        }
        return containNumber;
    }

    public boolean isQuoteAdjacent(String p_String) {
        boolean ret = false;
        String temp = p_String;
        temp = temp.replace("\"\"", "");
        if (temp.indexOf("\"") == -1) {
            ret = true;
        }

        return ret;

    }

    public boolean isQuoteContained(String p_String) {
        boolean ret = false;
        if (p_String == null || p_String.equals("")) {
            return false;
        }
        if (p_String.indexOf("\"") > -1) {
            ret = true;
        }
        return ret;

    }

    public boolean readCSVFileTitle() throws IOException, Exception {
        String strValue = "";
        boolean isLineEmpty = true;
        do {
            if (!readCSVNextRecord()) {
                return false;
            }
            if (vContent.size() > 0) {
                strValue = (String) vContent.get(0);
            }
            for (String str : vContent) {
                if (str != null && !str.equals("")) {
                    isLineEmpty = false;
                    break;
                }
            }

        } while (strValue.trim().startsWith("#") || isLineEmpty);
        return true;
    }
}